package com.xxxx.controller;

import com.github.pagehelper.PageInfo;
import com.xxxx.exception.UserNoLoginException;
import com.xxxx.pojo.Comment;
import com.xxxx.pojo.Order;
import com.xxxx.pojo.ResponseResult;
import com.xxxx.service.CommentService;
import com.xxxx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author Lionel
 * @Date 2021/3/11 16:41
 * Created by IntelliJ IDEA 2020.1.
 * Project: house
 * Description：订单控制类
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CommentService commentService;

    /**
     * 跳转至我的订单页面
     * @return 我的订单页面
     */
    @GetMapping("/toOrder")
    public String toOrder() {
        return "order";
    }

    /**
     * 跳转至商家订单页面
     * @return 商家订单页面
     */
    @GetMapping("/toHkOrder")
    public String toHkOrder() {
        return "hk_order";
    }

    @PostMapping("/getAllOrder")
    @ResponseBody
    public ResponseResult<PageInfo<Order>> getAllOrder (
            HttpSession session,
            @RequestParam(required = false,defaultValue = "1",value = "pageNum")Integer currentPage
    ) {
        ResponseResult<PageInfo<Order>> result = new ResponseResult<>();
        if (session == null) {
            throw new UserNoLoginException("用户未登录");
        } else {
            PageInfo<Order> list = orderService.getAllOrder(session, currentPage);
            result.setData(list);
        }
        return result;
    }

    @PostMapping("/getOrderByID")
    @ResponseBody
    public ResponseResult<Order> getOrderByID (
            @RequestParam("orderID") int id
    ) {
        ResponseResult<Order> result = new ResponseResult<>();
        Order order = orderService.getOrderByID(id);
        result.setData(order);
        return result;
    }

    @PostMapping("/deleteOrderByID")
    @ResponseBody
    public ResponseResult<Void> deleteOrderByID (
            @RequestParam("orderID") int id
    ) {
        orderService.deleteOrderByID(id);
        return new ResponseResult<>();
    }

    @PostMapping("/getOrderListByHKID")
    @ResponseBody
    public ResponseResult<List<Order>> getOrderListByHKID (
            HttpSession session
    ) {
        ResponseResult<List<Order>> result = new ResponseResult<>();
        List<Order> list = orderService.getOrderListByHKID(session);
        result.setData(list);
        return result;
    }

    @PostMapping("/acceptOrder")
    @ResponseBody
    public ResponseResult<Void> acceptOrder (
            @RequestParam("id") int id
    ) {
        orderService.updateOrderStatusByID(id, 1);
        return new ResponseResult<>();
    }

    @PostMapping("/finishOrder")
    @ResponseBody
    public ResponseResult<Void> finishOrder (
            @RequestParam("id") int id
    ) {
        orderService.updateOrderStatusByID(id, 0);
        return new ResponseResult<>();
    }

    @PostMapping("/cancelOrder")
    @ResponseBody
    public ResponseResult<Void> cancelOrder (
            @RequestParam("id") int id
    ) {
        orderService.cancelOrder(id);
        return new ResponseResult<>();
    }

    @PostMapping("/assessOrder")
    @ResponseBody
    public ResponseResult<Void> assessOrder (
            @RequestParam("orderID") int orderID,
            @RequestParam("commentStar") int commentStar,
            @RequestParam("commentContent") String commentContent
    ) {
        Comment comment = new Comment();
        comment.setOrderID(orderID);
        comment.setCommentStar(commentStar);
        comment.setCommentContent(commentContent);
        Date now = new Date();
        comment.setCommentTime(now);
        commentService.insertComment(comment);
        return new ResponseResult<>();
    }

}
